Data visualization with plotly, using a coding assistant

Visualization
Rosling
gapminder
ggplot2
plotly
Grammar of Graphics
Coding assistant
Published

January 16, 2026

M1 MIDS/MFA/LOGOS

Université Paris Cité

Année 2025

Course Homepage

Moodle

Objectives

Prompts (first draft)

ImportantPrompt

Load the gapminder package

NoteAnswer

Adding a code chunk to load the gapminder package after the preamble.

Code
Loading required package: ggplot2

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
Code

Added a code chunk to load the gapminder package. It appears after the preamble include and will load the library when the document is rendered. The chunk is configured to echo the code and evaluate it.

ImportantPrompt

Using package plotly, build an animated plot (each frame corresponding to a value in colum Year) mapping column gdpPercap to X-axis and lifeExpectancy to Y-axis

NoteAnswer

Creating an animated plotly plot. Checking the current file and the gapminder dataset structure.

Adding plotly and creating the animated plot

Code
# Create animated plotly visualization
# X-axis: gdpPercap, Y-axis: lifeExp, Frame: year
(gapminder |>
  ggplot() +
  aes(
    x = gdpPercap, 
    y = lifeExp, 
    frame = year, 
    color = continent, 
    size = pop, 
    text = country) +
  geom_point(alpha = 0.6) +
  scale_x_log10() +
  scale_size_area(
    max_size = 15,
    labels = scales::label_number(scale = 1/1e6, suffix = " M"),
    guide = guide_legend(title = "Population")
  ) +
  guides(color = "none") +
  labs(
    x = "GDP per Capita",
    y = "Life Expectancy",
    title = "Gapminder: Life Expectancy vs GDP per Capita (Animated)"
  )) |>
  ggplotly(height = 500, width = 750)

Added an animated plotly visualization:

Loads required packages: plotly and ggplot2 (in addition to gapminder)

Creates the animated plot with:

  • X-axis: gdpPercap (GDP per capita)
  • Y-axis: lifeExp (Life expectancy)
  • Frame: year (enables animation by year)
  • Log scale on the x-axis (common for GDP)

Labels and title

The frame = year aesthetic in the aes() call makes plotly animate by year. When rendered, you’ll see a play button to animate through the years, showing how countries move across the GDP per capita vs. life expectancy space over time.

Second thoughts

  • Metadata (FAIRness)
  • Commenting
    • Spelling out the message
    • Data criticism
    • Improving accessibility
    • Information released after hovering
  • Navigating the plot
    • Using plotly
    • The case for interactive dashboards
      • shiny
      • dash

Accessing gapminder metadata

ImportantPrompt

Where can I find metadata concerning the gapminder dataset?

NoteAnswer

Metadata about the gapminder dataset can be found in several places:

1. Package documentation

The most direct way to access metadata is through R’s help system:

Code
# View the dataset documentation
?gapminder

# Or equivalently
# help(gapminder)

2. Package vignettes

The gapminder package may include vignettes with detailed information:

Code
# List available vignettes
browseVignettes("gapminder")

# Or view a specific vignette
vignette("gapminder", package = "gapminder")

3. Dataset structure and attributes

You can inspect the dataset structure and any stored metadata:

Code
# View dataset structure
str(gapminder)
# View attributes (may contain metadata)
attributes(gapminder)
# Quick overview
tibble::glimpse(gapminder)

4. Online resources

5. Package description

Code
# Get package information
packageDescription("gapminder")
Package: gapminder
Title: Data from Gapminder
Version: 1.0.0
Authors@R: person("Jennifer", "Bryan", , "jenny@posit.co", role =
        c("aut", "cre"), comment = c(ORCID = "0000-0002-6983-2759"))
Description: An excerpt of the data available at Gapminder.org. For
        each of 142 countries, the package provides values for life
        expectancy, GDP per capita, and population, every five years,
        from 1952 to 2007.
License: CC0
URL: https://github.com/jennybc/gapminder,
        https://www.gapminder.org/data/,
        https://doi.org/10.5281/zenodo.594018,
        https://jennybc.github.io/gapminder/
BugReports: https://github.com/jennybc/gapminder/issues
Depends: R (>= 3.1.0)
Imports: tibble
Suggests: dplyr, ggplot2, knitr, rmarkdown, testthat (>= 3.0.0)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
Config/testthat/edition: 3
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2023-03-09 18:35:33 UTC; jenny
Author: Jennifer Bryan [aut, cre]
        (<https://orcid.org/0000-0002-6983-2759>)
Maintainer: Jennifer Bryan <jenny@posit.co>
Repository: CRAN
Date/Publication: 2023-03-10 09:50:08 UTC
Built: R 4.4.0; ; 2024-05-30 14:53:13 UTC; unix
RemoteType: standard
RemotePkgRef: gapminder
RemoteRef: gapminder
RemoteRepos: https://cloud.r-project.org
RemotePkgPlatform: source
RemoteSha: 1.0.0

-- File: /home/boucheron/R/x86_64-pc-linux-gnu-library/4.4/gapminder/Meta/package.rds 
Code
# Get package citation
citation("gapminder")
To cite package 'gapminder' in publications use:

  Bryan J (2023). _gapminder: Data from Gapminder_. R package version
  1.0.0, <https://CRAN.R-project.org/package=gapminder>.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {gapminder: Data from Gapminder},
    author = {Jennifer Bryan},
    year = {2023},
    note = {R package version 1.0.0},
    url = {https://CRAN.R-project.org/package=gapminder},
  }

This answer is mildly satisfactory. But what are we expecting concerning metadata?

  • Data sources
  • Data quality
  • Data

Gapminder documentation

The Gapminder foundation

A beautiful animation is offered on the Gapminder foundation website. A warning concerning data quality is available.

See Blog post on this issue

Life Expectancy at Birth Gapminder data

SDMX

DDF Model

DDFQL

Knowledge graphs

Playing with the palette

The case for interactive dashboards